﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Client
{
    class PayConnandConfigure : IPayConfigure
    {
        /// <summary>
        /// AppId 配置名称
        /// </summary>
        private const string AppId_SettingName = "Pay_AppId";
        /// <summary>
        /// AppKey 配置名称
        /// </summary>
        private const string AppKey_SettingName = "Pay_AppKey";
        /// <summary>
        /// AppSecret 配置名称
        /// </summary>
        private const string AppMerchantId_SettingName = "Pay_MerchantId";
        /// <summary>
        /// AppServerUrl 配置名称
        /// </summary>
        private const string AppServerUrl_SettingName = "Pay_ServerUrl";


        public string AppId { get; set; }

        public string AppKey { get; set; }

        public string MerchantId { get; set; }

        public string ServerUrl { get; set; }

        

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            AppId = "";
            AppKey = "";
            MerchantId = "";
            ServerUrl = "";
        }

        /// <summary>
        /// 是否已配置
        /// </summary>
        /// <returns></returns>
        public bool IsConfig()
        {
            return !string.IsNullOrWhiteSpace(AppId)
                && !string.IsNullOrWhiteSpace(AppKey)
                && !string.IsNullOrWhiteSpace(MerchantId)
                 && !string.IsNullOrWhiteSpace(ServerUrl);
        }

        /// <summary>
        /// 设置配置
        /// </summary>
        /// <param name="input"></param>
        /// <param name="currentValue"></param>
        /// <param name="setAct"></param>
        private void SetConfig(string input, string currentValue, Action<string> setAct)
        {
            if (string.IsNullOrWhiteSpace(currentValue))
            {
                Console.Write(input + ">");
                string cmd = Console.ReadLine();
                if (!string.IsNullOrWhiteSpace(cmd))
                {
                    setAct(cmd.Trim());
                }
                else
                {
                    SetConfig(input, "", setAct);
                }
            }
        }

        /// <summary>
        /// 设置配置
        /// </summary>
        public void SetConfig()
        {
            SetConfig("ServerUrl", ServerUrl, (c) => { ServerUrl = c; });
            SetConfig("AppId", AppId, (c) => { AppId = c; });
            SetConfig("AppKey", AppKey, (c) => { AppKey = c; });
            SetConfig("MerchantId", MerchantId, (c) => { MerchantId = c; });
        }

        /// <summary>
        /// 
        /// </summary>
        public void ShowCinfig()
        {
            ShowCinfig("ServerUrl", ServerUrl);
            ShowCinfig("AppId    ", AppId);
            ShowCinfig("AppKey   ", AppKey);
            ShowCinfig("MerchantId", MerchantId);
        }

        private void ShowCinfig(string name, string value)
        {
            if (value == null)
            {
                value = "";
            }
            Console.WriteLine(name + " : " + value);
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns></returns>
        private static string GetAppSetting(string name)
        {
            return ConfigurationManager.AppSettings.Get(name);
        }


        public void LoadConfig()
        {
            ServerUrl = GetAppSetting(AppServerUrl_SettingName);
            AppId = GetAppSetting(AppId_SettingName);
            AppKey = GetAppSetting(AppKey_SettingName);
            MerchantId = GetAppSetting(AppMerchantId_SettingName);
        }
    }
}
